package com.ywq.app.biz.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.ywq.app.biz.ArtAuctionBiz;
import com.ywq.app.biz.ArtPicBiz;
import com.ywq.app.biz.IdentifierBiz;
import com.ywq.app.biz.MailBiz;
import com.ywq.app.biz.MemberBiz;
import com.ywq.app.dao.ArtAuctionDao;
import com.ywq.app.dao.ArtOrdersDao;
import com.ywq.app.dao.ArtPicDao;
import com.ywq.app.domain.ArtArtist;
import com.ywq.app.domain.ArtAuction;
import com.ywq.app.domain.ArtAuctionPic;
import com.ywq.app.domain.ArtOrders;
import com.ywq.app.domain.ArtPic;
import com.ywq.app.domain.ArtReminder;
import com.ywq.app.domain.AuctionSo;
import com.ywq.app.domain.Member;
import com.ywq.app.domain.MemberAddress;
import com.ywq.app.domain.PicBidLog;
import com.ywq.common.constant.PageSize;
import com.ywq.common.exception.AppBusinessException;
import com.ywq.common.page.PageModel;
import com.ywq.common.util.Common;
import com.ywq.common.util.IdGenerator;

public class ArtAuctionBizImpl implements ArtAuctionBiz {

	private final static Logger logger = LoggerFactory
			.getLogger(ArtAuctionBizImpl.class);

	private ArtAuctionDao artAuctionDao;
	private ArtOrdersDao  artOrdersDao;
	private ArtPicDao  artPicDao;
	public ArtPicDao getArtPicDao() {
		return artPicDao;
	}

	public void setArtPicDao(ArtPicDao artPicDao) {
		this.artPicDao = artPicDao;
	}
	@Autowired
	private IdentifierBiz idBiz;
	@Autowired
	private MailBiz mailBiz;
	@Autowired
	private ArtPicBiz artPicBiz;
	
	@Autowired
	private MemberBiz memberBiz;
	
	public ArtOrdersDao getArtOrdersDao() {
		return artOrdersDao;
	}

	public void setArtOrdersDao(ArtOrdersDao artOrdersDao) {
		this.artOrdersDao = artOrdersDao;
	}

	public ArtAuctionDao getArtAuctionDao() {
		return artAuctionDao;
	}

	public void setArtAuctionDao(ArtAuctionDao artAuctionDao) {
		this.artAuctionDao = artAuctionDao;
	}
	
	// 主题列表
	@Override
	public PageModel getArtAuctionList(Integer page) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_LIST);
		map.put("pageSize", PageSize.PAGE_SIZE_LIST);
		map.put("isShow", "true");
		int count = artAuctionDao.getArtAuctionListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		List<ArtAuction>  artAuctionList= formatTime(artAuctionDao.getArtAuctionList(map));
		pm.setList(artAuctionList);
		pm.setPageSize(PageSize.PAGE_SIZE_LIST);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_LIST - 1)
				/ PageSize.PAGE_SIZE_LIST);
		return pm;
	}

	// 添加主题
	@Transactional(rollbackForClassName = {"com.ywq.common.exception.AppBusinessException;"}, timeout = 10)
	@Override
	public void insertArtAuction(ArtAuction artAuction) throws AppBusinessException {
		//String id = IdGenerator.createIdentifier();
		String id = idBiz.generateId("art_auction");
		artAuction.setIdentifier(id);
		artAuctionDao.insertArtAuction(artAuction);
	}

	// 更新主题
	@Override
	public void updateArtAuction(ArtAuction artAuction) throws AppBusinessException {
		artAuctionDao.updateArtAuction(artAuction);
	}

	// 前台查询主题当前
	@Override
	public List<ArtAuction> getNowAuctionList() throws AppBusinessException {
		List<ArtAuction> nowList = null;
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("isShow", "true");
			nowList = artAuctionDao.getNowAuctionList(map);
			if (nowList.size() > 0) {
				nowList=formatTime(nowList);
			}
		return nowList;
	}

	// 前台查询主题old
	@Override
	public List<ArtAuction> getOldAuctionList() throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isShow", "true");
		return artAuctionDao.getOldAuctionList(map);

	}

	// 按个数查询历史
	@Override
	public List<ArtAuction> getOldAuctionListBySize(Integer size)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isShow", "true");
		map.put("pageSize", size);
		map.put("startId", 0);
		return artAuctionDao.getOldAuctionList(map);

	}

	// 历史分页

	@Override
	public PageModel getRBackList(Integer page) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_ART_OLD);
		map.put("pageSize", PageSize.PAGE_ART_OLD);
		map.put("isShow", "true");
		int count = artAuctionDao.getOldAuctionListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		List<ArtAuction> oldList =artAuctionDao.getOldAuctionList(map);
		oldList=formatTime(oldList);
		pm.setList(oldList);
		pm.setPageSize(PageSize.PAGE_ART_OLD);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_ART_OLD - 1)
				/ PageSize.PAGE_ART_OLD);
		return pm;
	}

	// 格式化时间
	@Override
	public List<ArtAuction> formatTime(List<ArtAuction> rList) throws AppBusinessException {
		try {
			for (ArtAuction art : rList) {
				SimpleDateFormat secData = new SimpleDateFormat("yyyy-MM-dd");
				SimpleDateFormat secTime = new SimpleDateFormat("HH:mm:ss");
				Date sDate = secData.parse(art.getStartDate());
				Date eDate = secData.parse(art.getEndDate());
				Date sTime = secTime.parse(art.getStartTime());
				Date eTime = secTime.parse(art.getEndTime());
				art.setsDate(sDate);
				art.seteDate(eDate);
				art.setsTime(sTime);
				art.seteTime(eTime);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return rList;
	}
	
	
	// 格式化时间
	@Override
	public ArtAuction formatTimeAuction(ArtAuction art) throws AppBusinessException {
		try {
			
				SimpleDateFormat secData = new SimpleDateFormat("yyyy-MM-dd");
				SimpleDateFormat secTime = new SimpleDateFormat("HH:mm:ss");
				Date sDate = secData.parse(art.getStartDate());
				Date eDate = secData.parse(art.getEndDate());
				Date sTime = secTime.parse(art.getStartTime());
				Date eTime = secTime.parse(art.getEndTime());
				art.setsDate(sDate);
				art.seteDate(eDate);
				art.setsTime(sTime);
				art.seteTime(eTime);
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return art;
	}
	//主题详情
	/*@Override
	public ArtAuction getInAuctionById(String id,AuctionSo auctionSo,String seqtype,String seq)throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("identifier", id);
		map.put("isShow", "true");
		ArtAuction artAuction =artAuctionDao.getInAuctionById(map);
		artAuction=formatTimeAuction(artAuction);
		//增加关联的作品
		Map<String, Object> mapPic = new HashMap<String, Object>();
		mapPic.put("auctionId", id);
		mapPic.put("isShow", "true");
		if("price".equals(seqtype)){
		  mapPic.put("seqtype", "price");
		}
		if("maxPrice".equals(seqtype)){
			  mapPic.put("seqtype", "maxPrice");
			}
		if("nowPrice".equals(seqtype)){
			  mapPic.put("seqtype", "nowPrice");
			}
		if("lastPrice".equals(seqtype)){
			  mapPic.put("seqtype", "lastPrice");
			}
		//从大到小从小到大
		if("true".equals(seq) ){
			mapPic.put("seq", "desc");
		}else if("false".equals(seq)){
			mapPic.put("seq", "ASC");
		}
		if(auctionSo!=null){
			mapPic.put("classId", auctionSo.getClassId());
		}
		//List<ArtAuctionPic> artPicList =artAuctionDao.getArtAuctionPicById(mapPic);
		
		artAuction.setAuctionPicList(artPicList);
		return artAuction;
		
	}*/
	
	@Override
	public ArtAuction getInAuctionByAucId(String id,AuctionSo auctionSo,String seqtype,String seq)throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("identifier", id);
		map.put("isShow", "true");
		ArtAuction artAuction =artAuctionDao.getInAuctionById(map);
		artAuction=formatTimeAuction(artAuction);
		//增加关联的作品
		Map<String, Object> mapPic = new HashMap<String, Object>();
		mapPic.put("auctionId", id);
		mapPic.put("isShow", "true");
		if("price".equals(seqtype)){
		  mapPic.put("seqtype", "price");
		}
		if("maxPrice".equals(seqtype)){
			  mapPic.put("seqtype", "maxPrice");
			}
		if("nowPrice".equals(seqtype)){
			  mapPic.put("seqtype", "nowPrice");
			}
		if("lastPrice".equals(seqtype)){
			  mapPic.put("seqtype", "lastPrice");
			}
		if("myPrice".equals(seqtype)){
			mapPic.put("seqtype", "myPrice");
		}
		if("myLove".equals(seqtype)){
			mapPic.put("seqtype", "myLove");
		}
		//从大到小从小到大
		if("true".equals(seq) ){
			mapPic.put("seq", "desc");
		}else if("false".equals(seq)){
			mapPic.put("seq", "ASC");
		}
		if(auctionSo!=null){
			mapPic.put("classId", auctionSo.getClassId());
			mapPic.put("memberId", auctionSo.getMemberId());
			List<ArtPic> artPicList =artAuctionDao.getArtAuctionPicByAucId(mapPic);
			artAuction.setaPicList(artPicList);
		}
		return artAuction;
		
	}
	@Override
	public PicBidLog getSumBidPirceLog(String auctionId)throws AppBusinessException {
		PicBidLog sumMaxPirce =artAuctionDao.getSumBidPirceLog(auctionId);
		return sumMaxPirce;
		
	}
	
	//增加主题关联图片
	public void insertArtAuctionPic(ArtAuctionPic artAuctionPic)throws AppBusinessException {
		artAuctionDao.insertArtAuctionPic(artAuctionPic);
	}
	//某一主题关联的作品
	@Override
	public PageModel getModelRpics(String auctionId,Integer page) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("auctionId", auctionId);
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_PIC);
		map.put("pageSize", PageSize.PAGE_SIZE_PIC);
		int count = artAuctionDao.getArtAuctionPicByIdCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		List<ArtAuctionPic> artPicList =artAuctionDao.getArtAuctionPicById(map);
		pm.setList(artPicList);
		pm.setPageSize(PageSize.PAGE_SIZE_PIC);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_PIC - 1)/ PageSize.PAGE_SIZE_PIC);
		return pm;
	}
	//
	@Override
	public void insertArtReminder(ArtReminder artReminder) throws AppBusinessException {
		artAuctionDao.insertArtReminder(artReminder);
	}
	@Override
	public PageModel getArtAuctionListForMisc(Integer page,Integer pageSize) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * pageSize);
		map.put("pageSize",pageSize);
		int count = artAuctionDao.getArtAuctionListCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(artAuctionDao.getArtAuctionList(map));
		pm.setPageSize(pageSize);
		pm.setTotalRecord(count);
		pm.setTotalPage((count +pageSize - 1)
				/ pageSize);
		return pm;
	}
	//作废
	@Override
	public PageModel getModelRpicsForMisc(String auctionId,Integer page,Integer pageSize) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("auctionId", auctionId);
		map.put("startId", (page - 1) * pageSize);
		map.put("pageSize", pageSize);
		int count = artAuctionDao.getArtAuctionPicByIdCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		List<ArtAuctionPic> artPicList =artAuctionDao.getArtAuctionPicById(map);
		pm.setList(artPicList);
		pm.setPageSize(pageSize);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + pageSize - 1)/ pageSize);
		return pm;
	}
	//
	@Override
	public List<ArtAuctionPic> getAuctionPicListByAid(String auctionId)throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("auctionId", auctionId);
		List<ArtAuctionPic> artPicList =artAuctionDao.getArtAuctionPicById(map);
		return artPicList;
	}
	
	@Transactional(rollbackForClassName = {"com.ywq.common.exception.AppBusinessException;"}, timeout = 10)
	@Override
	public void insertArtOrders(ArtOrders artOrders) throws AppBusinessException {
		if(artOrders !=null){
			artOrders.setType("auction");
			
			//未付款
			artOrders.setIsRead(false);
			/*List<MemberAddress> mAddress = memberBiz.getAddressListByMemberId(artOrders.getMemberId());
			if(){
				
			}*/
			ArtPic artPic =artPicBiz.getArtPic(artOrders.getTypeId());
			artOrders.setTitle("竞购作品："+artPic.getTitle());
			Boolean isForward=artPic.getIsForward();
			//解锁
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("memberId", artOrders.getMemberId());
			unLockAccount(map);
			if(!isForward){
				//变更不可以买卖
				artPicBiz.noForwardPic(artPic.getIdentifier());
				//添加订单
				String id = "SA"+idBiz.generateId("art_orders");
				artOrders.setId(id);
				artOrders.setStatus("01");//下单成功
				//artOrders.setRemark();//地址
				artOrdersDao.insertArtOrders(artOrders);
				//发送邮件]
				Member member =memberBiz.getMemberById(artOrders.getMemberId());
				 /* member.setId("shanghaiZhangbOId");
				  member.setEmail("4112910@qq.com");*/
				//float sucprice =artOrders.getSucPrice();
				mailBiz.sendEmail(artOrders.getTypeId(),  member, "恭喜您，竞拍得到一幅您要的作品。", "您在笔飞商城已成功竞拍一幅作品，作品编号为："+artOrders.getTypeId()+"请您登陆笔飞商城查看。");
			}else{
				String id = idBiz.generateId("art_orders");
				artOrders.setId(id);
				artOrders.setStatus("02");//下单失败
				artOrdersDao.insertArtOrders(artOrders);
			}
		}
	}
	//解锁
	@Override
	public void unLockAccount(Map<String, Object> map)throws AppBusinessException {
		String memberId=(String)map.get("memberId");
		memberBiz.unLockAccount(memberId);
	}
	//枷锁
	@Override
	public void lockAccount(Map<String, Object> map)throws AppBusinessException {
		String memberId=(String)map.get("memberId");
		memberBiz.lockAccount(memberId);
	}
	
	 //查询某作品 出价纪录
    @Override
    public List<PicBidLog> getLogListBid(String picId) throws AppBusinessException {
    	if(!Common.emptyStr(picId)){
	    	Map<String, Object> map = new HashMap<String, Object>();
	    	map.put("picId", picId);
			return artAuctionDao.getLogListBid(map);
    	}
    	return null;
	}
	//主题详情(不包括作品)
	@Override
	public ArtAuction getAuctionById(String id)throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("identifier", id);
		ArtAuction artAuction =artAuctionDao.getInAuctionById(map);
		return artAuction;
	}
	//删除展览馆与作品
	@Transactional(rollbackForClassName = {"com.ywq.common.exception.AppBusinessException;"}, timeout = 10)
	@Override
	public void deleteAuctionPic(String picId,String auctionId)throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("picId", picId);
		map.put("auctionId", auctionId);
		artAuctionDao.deleteAuctionPic(map);
		artAuctionDao.deletePic(map);
	}
	//发现下单
	@Transactional(rollbackForClassName = {"com.ywq.common.exception.AppBusinessException;"}, timeout = 10)
	@Override
	public String insertFindArtOrders(ArtOrders artOrders) throws AppBusinessException {
		String id ="SA"+ idBiz.generateId("art_orders");
		if(artOrders !=null && artOrders.getSucPrice()!=0){
			artOrders.setIsRead(false);
			ArtPic artPic =artPicBiz.getArtPic(artOrders.getTypeId());
			Boolean isForward=artPic.getIsForward();
			if(!isForward){
				//变更不可以买卖
				//artPicBiz.noForwardPic(artPic.getIdentifier());
				Map<String, Object> map = new HashMap<String, Object>();
		    	map.put("picId", artPic.getIdentifier());
		    	map.put("isForward", true);
		    	map.put("type", "find");
		    	artPicDao.noForwardPic(map);
				//添加订单
				artOrders.setId(id);
				artOrders.setStatus("01");//下单成功
				artOrdersDao.insertArtOrders(artOrders);
				//发送邮件]
				Member member =memberBiz.getMemberById(artOrders.getMemberId());
				/*Member member =new Member();
				  member.setId("shanghaiZhangbOId");
				  member.setEmail("4112910@qq.com");*/
				//float sucprice =artOrders.getSucPrice();
				mailBiz.sendEmail(artOrders.getTypeId(),  member, "恭喜您在笔飞商城成功购买了一幅作品", "您在笔飞商城已成功购买一幅作品，作品编号为："+artOrders.getTypeId()+"请您登陆笔飞商城查看。");
			}else{
				artOrders.setId(id);
				artOrders.setStatus("02");//下单失败
				artOrdersDao.insertArtOrders(artOrders);
			}
		}
		return id;
	}
}
